import Vue from 'vue'
import { BASE_REQUEST_PATH, BASE_URL_NAME } from '@/store/mutation-types'

/**
 * 获取服务端基础请求路径url
 * @returns {string}
 */
export function getBaseRequestUrl () {
  return Vue.ls.get(BASE_URL_NAME) + BASE_REQUEST_PATH
}

/**
 * 判断是否为空
 * @param param
 * @returns {boolean}
 */
export function emptyObject (param = {}) {
  if (param === null || param === '') {
    return true
  }
  if (JSON.stringify(param) === '{}') {
    return true
  }
  return false
}

/**
 * 判断file是否为excel
 * @param file
 * @returns {boolean}
 */
export function checkExcelType (file) {
  const fileName = file.name.split('.')
  const fileNameElement = fileName[fileName.length - 1]
  const types = ['xlsx', 'xls', 'xltx', 'xlt', 'xlsm', 'xlsb', 'xltm', 'csv']
  return types.indexOf(fileNameElement) > -1
}

/**
 * 获取excel导出 文件名
 * @param excel 对象
 * @returns {string}
 */
export function getExcelFileName (excel) {
  if (emptyObject(excel)) {
    return '下载文件.xls'
  }
  const name = excel.excelName || '下载文件'
  let type = 'xls'
  if (!emptyObject(excel.excelType)) {
    const excelType = excel.excelType.trim()
    if (excelType === 'xlsx') {
      type = 'xlsx'
    }
  }
  return name.trim() + '.' + type
}

/**
 * @function escapeHTML 转义html脚本 < > & " '
 * @param a
 * @returns {string}
 */
export function escapeHTML (a) {
  a = '' + a
  return a.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;')
}

/**
 * @function unescapeHTML 还原html脚本 < > & " '
 * @param a
 * @returns {string}
 */
export function unescapeHTML (a) {
  a = '' + a
  return a.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&apos;/g, '\'')
}

/**
 * @function 密码规则
 * @param value
 * @description 必须包含数字、特殊符号、字母，长度8-16
 * @returns {boolean}
 */
export function passwordRule (value) {
  const m = /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([,.<>{}~!@#$%^&*_])+$)(?!([^(A-Z,.<>{}~!@#$%^&*_)])+$)(?!([^(a-z,.<>{}~!@#$%^&*_)])+$)(?!([^(0-9,.<>{}~!@#$%^&*_)])+$)(?!([^(0-9a-z)])+$)(?!([^(A-Za-z)])+$)(?!([^(0-9A-Z)])+$)^.{8,16}$/
  if (value.match(m)) {
    return true
  } else {
    return false
  }
}

/**
 * @function 用户名规则
 * @param value
 * @description 只能由字母、数字、下划线组成，长度5-10
 * @returns {boolean}
 */
export function usernameRule (value) {
  const m = /^\w{5,10}$/
  if (value.match(m)) {
    return true
  } else {
    return false
  }
}

/**
 * @function 手机号码规则
 * @param value
 * @description  (区号)+11位手机号码
 * @returns {boolean}
 */
export function phoneRule (value) {
  const m = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
  // m2 为中间4位加密
  // const m2 = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[*]{4}[0-9]{4}$/
  if (value.match(m)) {
    return true
  } else {
    return false
  }
}

/**
 * 状态类型 转换成图标展示规则
 * @param valve
 * @returns {convert: number, title: string}
 */
export function statusConvert (value, fmtArr) {
  fmtArr = fmtArr || ['ok', 'fail']
  const convert = { 'convert': value, title: fmtArr[0] }
  if (value === 1) {
    convert.title = fmtArr[0]
  } else {
    convert.title = fmtArr[1]
  }
  return convert
}
